define(['app'], function(app) {
	app.controller('checkedCtrl', function($rootScope, $scope, $stateParams, $http,  $modal, $log) {
        console.log($rootScope.alllimits);
        $http({
            url:"/OSRS/check/showResearch",
            method:"get"
        }).then(function(res){
            /*console.log(res.data)*/
            $scope.data = res.data
            $scope.pageNow = res.data.pageNow;
            $scope.totalRecords = res.data.totalRecords;
            layui.use(['element', 'form', 'laypage'], function() {
                window.form = layui.form;
                var laypage = layui.laypage,
                    element = layui.element;

                form.render();
                //监听提交
                form.on('submit(addrole)', function(data) {
                    layer.msg(JSON.stringify(data.field));
                    return false;
                });
                //分页
                laypage.render({
                    elem: 'pagination', //ID，不加 # 号
                    limit: 7, //每页条数
                    //limits:[10, 20, 30, 40, 50],//配合limit展示下拉选项
                    count: $scope.totalRecords,//页总数，从服务端得到
                    curr: $scope.pageNow,
                    groups: 3, //连续出现的页码个数
                    prev: "上一页",
                    next: "下一页",
                    first: "首页",
                    last: "尾页",
                    layout: ['prev', 'page', 'next'],
                    hash: false,
                    jump: function(obj, first) {
                        $http({
                            url:"/OSRS/check/researchlist?pageNow=" + obj.curr + "&pageSize=" + obj.limit,
                            method:"get"
                        }).then(function(res){
                            $scope.data = res.data
                            console.log(res.data)
                        },function(err){
                            console.log(err)
                        })
                    }
                });
            });
        },function(err){
            console.log(err)
        });
        layui.use(['element', 'form', 'laypage'], function() {
            window.form = layui.form;
            var laypage = layui.laypage,
                element = layui.element;

            form.render();
            //监听提交
            form.on('submit(addrole)', function(data) {
                layer.msg(JSON.stringify(data.field));
                return false;
            });
            //分页
            laypage.render({
                elem: 'pagination', //ID，不加 # 号
                limit: 10, //每页条数
                //limits:[10, 20, 30, 40, 50],//配合limit展示下拉选项
                count: 100, //数据总数，从服务端得到
                curr: 1,
                groups: 5, //连续出现的页码个数
                prev: "上一页",
                next: "下一页",
                first: "首页",
                last: "尾页",
                layout: ['prev', 'page', 'next'],
                hash: false,
                jump: function(obj, first) {
                    console.log(obj, first)
                }
            });
        });
        $scope.detailshow = false;
        $scope.detail = function (x) {
            $scope.detailshow = true;
            $http({
                url:"/OSRS/check/showDetail",
                method:"post",
                data: JSON.stringify(x)
            }).then(function(res){
                $scope.detailData = res.data
                console.log(res.data)
            },function(err){
                console.log(err)
            })
        }
        $scope.detailhide = function() {
            $scope.detailshow = false;
        };
        $scope.mysearch=function () {
            var inputs = document.getElementsByTagName("input")
            var researchDetailids="";
            for(var i = 0; i < inputs.length; i++) {
                if (inputs[i].type == 'search') {
                    researchDetailids += inputs[i].value;
                }
            }
            $http({
                url:"/OSRS/check/getResearch",
                method:"post",
                data: researchDetailids
            }).then(function(res){
                console.log(res.data)
                $scope.data = res.data
            },function(err){
                console.log(err)
            })
        }
        $scope.check = function(x) {
            //得到所有的input
            layer.confirm('确认要审核调查吗？', {
                icon: 3,
                title: '确认审核'
            }, function(researchDetailids) {
                //do something
                var inputs = document.getElementsByTagName("input")
                var researchDetailids="";
                var detailids="";
                for(var i = 0; i < inputs.length; i++) {
                    if (inputs[i].type == 'checkbox') {
                        if (inputs[i].checked) {
                            researchDetailids += inputs[i].value + ":";
                        }
                    }
                }
                console.log("确认审核")
                $http({
                    method: "post",
                    url: "/OSRS/check/alertResearchDetail",
                    data:researchDetailids+";"+x
                }).then(function(res) {
                    layer.confirm('审核成功', {
                        icon: 1,
                        btn: ['好'],
                        title: '提示'
                    }, function(index) {
                        layer.close(index);
                        $state.go('checked')
                    });
                }, function(err) {
                    layer.confirm('审核失败', {
                        icon: 2,
                        btn: ['好'],
                        title: '提示'
                    }, function(index) {
                        layer.close(index);
                    });
                });
				/* layer.close(index);*/
            });
        }
        $rootScope.detailModal = function(x) {
            //此处应该是根据x.id获取考评的具体细节数据，然后填充至modal中
            $http({
                method:"post",
                url:"/OSRS/check/showOneResearchDetail",
                data:x
            }).then(function(res){
                console.log(res.data)
				/*此处需要注意
				 * */
                $rootScope.detailDatas = res.data
                var modalInstance = $modal.open({
                    title: "调查明细",
                    keyboard: true,
                    controller: ModalInstanceCtrl,
                    resolve: {
                        items: function() {
                            return $rootScope.detailDatas;
                        }
                    },
                    windowClass: 'uncheck-Modal',
                    template: `
		<div class="layui-text">
			<p class="layui-layer-title">调查明细</p>
			<div ng-repeat="x in detailDatas track by $index" ng-if="x.title">
                   <h3 >{{x.title}}</h3>
                   <table class="layui-table" lay-skin="line" ng-repeat="p in x.poll track by $index" &ndash;&gt;>
                       <tr>
                             <td>{{p.question}}</td>
                             <td>
                                  <span class="star"  ng-repeat="x in stars0 | limitTo:p.answer track by $index"></span> {{p.answer}}分
                             </td>
                       </tr>
                   </table>
            </div>
            <div ng-repeat="x in detailDatas track by $index" ng-if="!x.title">
                   <div ng-repeat="p in x.poll track by $index" >
                        <h3>{{p.question}}</h3>
                        <textarea class="layui-textarea" name="" rows="" cols="" disabled>{{p.answer}}</textarea> 
                   </div>
            </div>
            <div class="layui-form-item">
				    <div class="layui-input-block" style="float: right">
				    	  <button class="layui-btn layui-btn-primary" ng-click="cancel()">取消</button>
				    </div>
				  </div>
		</div>		`
                });
            },function(err){
                layer.confirm('数据获取失败~', {
                    icon: 2,
                    btn: ['好'],
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                });
            })
            console.log("detailModal")

        }
		var ModalInstanceCtrl = function($scope, $modalInstance, items) {
			$rootScope.cancel = function() {
				$modalInstance.dismiss('cancel');
			};
		}
		
		
	});
	
});